<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.uid" clearable placeholder="用户ID" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.sex" placeholder="性别" clearable style="width: 120px" class="filter-item">
        <el-option v-for="item in sexOptions" :key="item.name" :label="item.name" :value="item.value" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      height="550"
      @sort-change="sortChange"
    >
      <el-table-column label="ID" prop="id" sortable="custom" align="center" width="80" :class-name="getSortClass('id')">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column label="头像" width="64" align="center">
        <template slot-scope="{row}">
          <el-image class="avatar-circular" :src="getAvatarImage(row.avatar)" fit="fit" />
        </template>
      </el-table-column>

      <el-table-column label="区域号" width="120" align="center">
        <template slot-scope="{row}">
          <span>{{ row.country_code }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" width="120" align="center">
        <template slot-scope="{row}">
          <span>{{ row.mobile }}</span>
        </template>
      </el-table-column>

      <el-table-column label="昵称" width="120" align="center">
        <template slot-scope="{row}">
          <span>{{ row.nickname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" width="120" align="center">
        <template slot-scope="{row}">
          <span>{{ getSex(row.userExtend.sex) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账号状态" class-name="status-col" width="120" align="center">
        <template slot-scope="{row}">
          <el-tag :type="getStatusType(row.status)">
            {{ getStatusString(row.status) }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="会员状态" class-name="status-col" width="120" align="center">
        <template slot-scope="{row}">
          <el-tag :type="getUserStatusType(row.user_status)">
            {{ getUserStatusString(row.user_status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" min-width="200" class-name="small-padding fixed-width">
        <template slot-scope="{row, $index}">
          <el-button type="primary" size="mini" @click="jump(`/user/profile/index?id=${row.id}`)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="flex flex-center">
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
  </div>
</template>

<script>
import {
  fetchUserList
} from '@/api/user.js'
import Pagination from '@/components/Pagination'

export default {
  name: 'UserList',
  components: {
    Pagination
  },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      submitLoading: false, // 提交中
      statusType: {
        '0': 'danger',
        '1': 'danger',
        '10': 'success'
      },
      statusString: {
        '0': '注销',
        '9': '待激活',
        '10': '正常'
      },
      userStatusType: {
        '-1': 'danger',
        '0': 'danger',
        '1': 'success',
        '10': 'danger'
      },
      userStatusString: {
        '0': '异常',
        '1': '正常',
        '10': '禁止登录'
      },
      listQuery: {
        page: 1,
        sort: '-id',
        uid: '',
        sex: '',
        limit: 10
      },
      sexOptions: [{
        name: '全部性别',
        value: ''
      },
      {
        name: '保密',
        value: -1
      },
      {
        name: '男',
        value: 1
      },
      {
        name: '女',
        value: 0
      }
      ],
      textMap: {
        update: '编辑',
        create: '添加'
      },
      currentUser: '',
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    getAvatarImage(url) {
      return this.$utils.getAvatarImage(url)
    },
    getList() {
      fetchUserList(this.listQuery).then(res => {
        this.listLoading = false
        this.list = res.data.items
        this.total = res.data.total
      }, rej => {
        this.listLoading = false
      })
    },
    getSex(sex) {
      if (sex == -1) return '保密'
      if (sex == 1) return '男'
      if (sex == 0) return '女'
    },
    getStatusType(status) {
      return this.statusType[status.toString()]
    },
    getStatusString(status) {
      return this.statusString[status.toString()]
    },
    getUserStatusType(status) {
      return this.userStatusType[status.toString()]
    },
    getUserStatusString(status) {
      return this.userStatusString[status.toString()]
    },
    getUserVipTypeStr(type) {
      return this.userVipTypeStr[type]
    },
    // 排序更新
    sortChange(data) {
      const {
        prop,
        order
      } = data
      if (prop === 'id') {
        this.sortByID(order)
      } else if (prop === 'sort') {
        this.sortBySort(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+id'
      } else {
        this.listQuery.sort = '-id'
      }
      this.getList()
    },
    sortBySort(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+sort'
      } else {
        this.listQuery.sort = '-sort'
      }
      this.getList()
    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort
      if (sort === `+${key}`) {
        return 'ascending'
      } else if (sort === `-${key}`) {
        return 'descending'
      }
    },
    jump(url) {
      this.$router.push(url)
    }
  }
}
</script>

<style>
  .el-dialog {
    width: 550px;
  }

  .cate-icon {
    width: 2.5rem;
    display: flex;
    justify-content: center;
    align-items: center;
  }

</style>
